import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  // 最大级别,对应组件，会显示在App.vue
  // 登录
  { path: '/login', component: () => import('@/views/MyLogin.vue') },
  // 注册
  { path: '/reg', component: () => import('@/views/MyReg.vue') },
  // 首页
  {
    path: '/',
    redirect: 'chart', // 重定向
    component: () => import('@/views/MyHome.vue'),
    children: [
      { path: 'chart', component: () => import('@/views/Chart.vue') },
      { path: 'user-info', component: () => import('@/views/user/UserInfo.vue') },
      { path: 'user-pwd', component: () => import('@/views/user/RePwd.vue') },
      { path: 'user-avatar', component: () => import('@/views/user/UserAvatar.vue') },
      { path: 'art-cate', component: () => import('@/views/article/CateList.vue') },
      { path: 'art-list', component: () => import('@/views/article/ArtList.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // to.path//表示即将要访问的路由地址
  // next（）放行
  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    // 说明访问的不是登录，也不是注册，判断是否又token
    if (store.state.user.token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
